package com.dream.base.ui;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.dream.base.BaseConstants;
import com.dream.base.R;
import com.dream.base.thread.WeakHandler;
import com.dream.base.tools.DialogUtil;
import com.dream.base.mvp.contract.BaseContract;
import com.dream.base.mvp.presenter.BasePresenter;
import com.dream.base.tools.SharedPreferencesUtil;
import com.dream.base.ui.widget.CommonTitleView;
import com.readystatesoftware.systembartint.SystemBarTintManager;


/**
 * Description: App's activity should extend this class
 *
 * @author: WangKunHui
 * date: 11/28/16 10:36 AM
 * <p>
 * Copyright©2016 by wang. All rights reserved.
 */
public abstract class BaseActivity<T extends BaseContract.BasePresenter> extends AppCompatActivity implements View.OnClickListener {

    protected String TAG = "BaseActivity";

    private LinearLayout rootLayout;

    protected CommonTitleView commonTitleView;

    private View commonDidiver;

    protected T mPresenter;

    /**
     * status bar transform color resource id
     */
    protected int colorRes = 0;

    /**
     * default loading dialog
     */
    private DialogUtil defaultLoadingDialog;

    private boolean isShowTitle = false;

    /**
     * 项目自定义dialog布局
     */
    private int loadingDialogLayout;

    private WeakHandler mWeakHandler;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        int aLong = SharedPreferencesUtil.getInt(this, BaseConstants.CURRENT_STYLE, 0);
        if (aLong != 0) {
            setTheme(aLong);
        }

        TAG = this.getClass().getSimpleName();

        beforeLoadActivity(savedInstanceState);

        mPresenter = initPresenter();

        getExtraData();

        isShowTitle = isShowTitleView();

        initView();

        findViews();

        initData();

        setListeners();

        loadingDialogLayout = getCustomLoadingLayout();
    }

    protected void findViews() {

    }

    protected void beforeLoadActivity(Bundle savedInstanceState) {

    }

    /**
     * 获取项目loading布局
     *
     * @return
     */
    protected int getCustomLoadingLayout() {
        return 0;
    }

    protected boolean isShowTitleView() {
        return false;
    }

    /**
     * set subclass's layout resource id
     *
     * @return resID
     */
    protected abstract int setLayoutRes();

    protected abstract void initData();

    protected void setListeners() {
    }


    /**
     * Initialization view
     */
    private void initView() {
        setContentView(R.layout.activity_base_layout);
        rootLayout = (LinearLayout) findViewById(R.id.base_root);
        commonTitleView = (CommonTitleView) findViewById(R.id.common_title);
        commonDidiver = findViewById(R.id.common_title_divider);

        if (isShowTitle) {
            commonTitleView.setVisibility(View.VISIBLE);
            if (isShowDivider()) {
                commonDidiver.setVisibility(View.VISIBLE);
            }
            initTitleView();
        }

        int subclassLayoutId = setLayoutRes();
        View inflate = LayoutInflater.from(this).inflate(subclassLayoutId, null);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        inflate.setLayoutParams(params);
        rootLayout.addView(inflate);
    }

    protected boolean isShowDivider() {
        return true;
    }

    protected void initTitleView() {
        commonTitleView.setOnLiftClick(new CommonTitleView.OnTitleClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    /**
     * Fetch data form Intent and execute extra options
     */
    protected void getExtraData() {
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    protected void showLoadingDialog() {
        showLoadingDialog(0);
    }

    protected void showLoadingDialog(int textRes) {

        if (defaultLoadingDialog == null) {
            defaultLoadingDialog = new DialogUtil(getContext(), loadingDialogLayout);
        }

        defaultLoadingDialog.setTextRes(textRes);
        defaultLoadingDialog.show();
    }

    protected void dismissLoadingDialog() {
        defaultLoadingDialog.dismiss();
    }

    /**
     * 初始化present
     *
     * @return
     */
    protected abstract T initPresenter();

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    protected Activity getInstance() {
        return this;
    }

    protected Context getContext() {
        return this;
    }

    protected void initHandler(Handler.Callback mCallback) {
        mWeakHandler = new WeakHandler(mCallback);
    }

    protected void sendEmptyMessage(int what) {
        if (null == mWeakHandler) {
            mWeakHandler = new WeakHandler(Looper.getMainLooper());
        }
        mWeakHandler.sendEmptyMessage(what);

    }

    protected void sendMessage(Message message) {
        if (null == mWeakHandler) {
            mWeakHandler = new WeakHandler(Looper.getMainLooper());
        }
        mWeakHandler.sendMessage(message);
    }

    protected void sendEmptyMessageDelay(int what, long delay) {
        if (null == mWeakHandler) {
            mWeakHandler = new WeakHandler(Looper.getMainLooper());
        }
        mWeakHandler.sendEmptyMessageDelayed(what, delay);

    }

    protected void sendMessageDelay(Message message, long delay) {
        if (null == mWeakHandler) {
            mWeakHandler = new WeakHandler(Looper.getMainLooper());
        }
        mWeakHandler.sendMessageDelayed(message, delay);

    }

    protected void post(Runnable runnable) {
        if (null == mWeakHandler) {
            mWeakHandler = new WeakHandler(Looper.getMainLooper());
        }

        mWeakHandler.post(runnable);
    }

    protected void postDelay(Runnable runnable, long delay) {
        if (null == mWeakHandler) {
            mWeakHandler = new WeakHandler(Looper.getMainLooper());
        }
        mWeakHandler.postDelayed(runnable, delay);

    }

    protected void removeMessage(int what) {
        if (null == mWeakHandler) {
            mWeakHandler = new WeakHandler(Looper.getMainLooper());
        }

        mWeakHandler.removeMessages(what);
    }

    protected void removeMessageCallBack(Runnable runnable) {
        if (null == mWeakHandler) {
            mWeakHandler = new WeakHandler(Looper.getMainLooper());
        }
        mWeakHandler.removeCallbacks(runnable);
    }
}
